<?php
/**
 * 文章管理控制器
 */
class ArticleController extends PlatformController
{
	/**
	 * 文章首页展示
	 */
	public function indexAction()
	{
		$article=Factory::M('ArticleModel');
		$artInfo=$article->getArticle();
		$this->assign('artInfo',$artInfo);
		//分页相关
		$rowsCount=$article->getRowsCount();
		$rowsPrePage=$GLOBALS['conf']['Page']['rowsPrePage'];
		$maxNum=$GLOBALS['conf']['Page']['maxNum'];
		$url="index.php?p=Back&c=Article&a=index";
		$page=new Page($rowsCount,$rowsPrePage,$maxNum,$url);
		$strPage=$page->show();
		$this->assign('strpage',$strPage);
		$this->display('index.html');
	}
	/**
	 * 添加文章
	 */
	public function addAction()
	{
		$category=Factory::M('CategoryModel');
		$cateInfo=$category->getCategory();
		$this->assign('cateInfo',$cateInfo);
		$this->display('add.html');
	}
	/**
	 * 处理文章添加
	 */
	public function dealAddAction()
	{
		$art=array();
		$art['title']=$this->escapeData($_POST['title']);
		$art['cate_id']=$_POST['cate_id'];
		$art['author']=$this->escapeData($_POST['author']);
		$art['art_desc']=$this->escapeData($_POST['art_desc']);
		$art['content']=addslashes($_POST['content']);
		//判断数据合法性
		if(empty($art['title']) || empty($art['author']) || empty($art['art_desc']) || empty($art['content']))
		{
			$this->jump('index.php?p=Back&c=Article&a=add',':(信息不完整!');
		}
		//判断是否有缩略图上传
		if($_FILES['thumb']['error'] != 4)
		{
			$upload=new Upload;
			$allow=array('image/png','image/jpg','image/jpeg','image/gif');
			$path=UPLOADS_DIR.'thumb/'.date('Ymd');
			if(!file_exists($path))
			{
				mkdir($path);
			}
			//上传
			$result=$upload->uploadAction($_FILES['thumb'],$allow,$path);
		
			if($result)
			{
				$art['thumb']=$result;
			}
			else
			{
				$error=Upload::$error;
				$this->jump('index.php?p=Back&c=Article&a=add',$error);
			}

		}
		else
		{
			$art['thumb']='default.jpg';
		}
		$article=Factory::M('ArticleModel');
		$result=$article->insertArt($art);
		if($result)
		{
			$this->jump('index.php?p=Back&c=Article&a=index');
		}
		else
		{
			$this->jump('index.php?p=Back&c=Article&a=add','未知错误,发布失败');
		}
	}
	public function editAction()
	{
		$art_id=$_GET['art_id'];
		$article=Factory::M('ArticleModel');
		$art=$article->getArticleById($art_id);
		$this->assign('art',$art);
		$category=Factory::M('CategoryModel');
		$cateInfo=$category->getCategory();
		$this->assign('cateInfo',$cateInfo);
		$this->display('edit.html');
	}
	
	/**
	 * 处理文章修改
	 */
	public function dealEditAction()
	{
		$art=array();
		$art['art_id']=$_POST['art_id'];
		$art['title']=$this->escapeData($_POST['title']);
		$art['cate_id']=$_POST['cate_id'];
		$art['author']=$this->escapeData($_POST['author']);
		$art['art_desc']=$this->escapeData($_POST['art_desc']);
		$art['content']=$_POST['content'];
		
		//判断数据合法性
		if(empty($art['title']) || empty($art['author']) || empty($art['art_desc']) || empty($art['content']))
		{
			$this->jump('index.php?p=Back&c=Article&a=edit',':(信息不完整!');
		}
		//判断是否有缩略图上传
		if($_FILES['thumb']['error'] != 4)
		{
			$upload=new Upload;
			$allow=array('image/png','image/jpg','image/jpeg','image/gif');
			$path=UPLOADS_DIR.'thumb/'.date('Ymd');
			if(!file_exists($path))
			{
				mkdir($path);
			}
			//上传
			$result=$upload->uploadAction($_FILES['thumb'],$allow,$path);
		
			if($result)
			{
				$art['thumb']=$result;
			}
			else
			{
				$error=Upload::$error;
				$this->jump('index.php?p=Back&c=Article&a=edit',$error);
			}

		}
		else
		{
			$art['thumb']=$_POST['thumb'];
		}
		$article=Factory::M('ArticleModel');
		$result=$article->updateArtById($art);
		if($result)
		{
			$this->jump('index.php?p=Back&c=Article&a=index');
		}
		else
		{
			$this->jump('index.php?p=Back&c=Article&a=edit','未知错误,发布失败');
		}
	}
	/**
	 * 删除文章动作
	 */
	public function delAction()
	{
		$art_id=$_GET['art_id'];
		$article=Factory::M('ArticleModel');
		$result=$article->delArticleById($art_id);
		if($result)
		{
			$this->jump('index.php?p=Back&c=Article&a=index');
		}
		else
		{
			$this->jump('index.php?p=Back&c=Article&a=index','未知错误,删除失败');
		}
	}
	public function delAllAction()
	{
		$art_id=implode(',',$_POST['art_id']);
		$article=Factory::M('ArticleModel');
		$result=$article->delAllArticle($art_id);
		if($result)
		{
			$this->jump('index.php?p=Back&c=Article&a=index');
		}
		else
		{
			$this->jump('index.php?p=Back&c=Article&a=index','未知错误,删除失败');
		}
	}
	public function recycleAction()
	{
		$article=Factory::M('ArticleModel');
		$artInfo=$article->getDeledArticle();
		$this->assign('artInfo',$artInfo);
		$this->display('recycle.html');
	}
	public function realDelAction()
	{
		$art_id=$_GET['art_id'];
		$article=Factory::M('ArticleModel');
		$result=$article->realDelArtById($art_id);
		if($result)
		{
			$this->jump('index.php?p=Back&c=Article&a=recycle');
		}
		else
		{
			$this->jump('index.php?p=Back&c=Article&a=recycle','未知错误,删除失败');
		}
	}
	public function realDelAllAction()
	{
		$art_id=$_POST['art_id'];
		$art_id=implode(',',$art_id);
		$article=Factory::M('ArticleModel');
		$result=$article->realDelAllArticle($art_id);
		if($result)
		{
			$this->jump('index.php?p=Back&c=Article&a=recycle');
		}
		else
		{
			$this->jump('index.php?p=Back&c=Article&a=recycle','未知错误,删除失败');
		}
	}
	public function recoverAction()
	{
		$art_id=$_GET['art_id'];
		$article=Factory::M('ArticleModel');
		$result=$article->recoverArtById($art_id);
		if($result)
		{
			$this->jump('index.php?p=Back&c=Article&a=recycle');
		}
		else
		{
			$this->jump('index.php?p=Back&c=Article&a=recycle','未知错误,发布失败');
		}
	}
	public function ifRecommendAction()
	{
		$art_id=$_GET['art_id'];
		$is_recommend=$_GET['is_recommend'];
		$pageNum=$_GET['pageNum'];
		$article=Factory::M('ArticleModel');
		$result=$article->updateArtRecommendById($art_id,$is_recommend);
		if($result)
		{
			$this->jump("index.php?p=Back&c=Article&a=index&pageNum=$pageNum");
		}
		else
		{
			$this->jump("index.php?p=Back&c=Article&a=index&pageNum=$pageNum",'未知错误,操作失败');
		}
	}
}